package top.codedance.iotp.iot.sdk;

import com.alibaba.fastjson.JSONObject;
import top.codedance.iotp.iot.sdk.po.sync.*;

import javax.xml.bind.DatatypeConverter;

public abstract class AbstractIotService implements IotService {

    /**
     * 还原结果
     * @param result
     * @return
     */
    protected JSONObject reductionData(JSONObject result){
        try {
            if (result.containsKey("status") && result.getIntValue("status") == 200) {
                if (result.containsKey("data")) {
                    JSONObject data = result.getJSONObject("data");
                    if (data.containsKey("hex")) {
                        String hex = data.getString("hex");
                        String[] hexs = hex.split("\\.");
                        for (int i = 0; i < hexs.length; i++) {
                            if (i == hexs.length - 1) {
                                data.put(hexs[i], new String(DatatypeConverter.parseHexBinary(data.getString(hexs[i]))));
                            } else {
                                data = data.getJSONObject(hexs[i]);
                            }
                        }
                    }
                }
            }
        } catch (Exception e){
        } finally {
            return result;
        }
    }

    /**
     * 获取服务端信息
     * @param baseParam
     * @return
     */
    @Override
    public abstract JSONObject serverInfomation(BaseParam baseParam);

    /**
     * 获取某个服务端的所有已注册的DeviceIds
     * @param baseParam
     * @return
     */
    @Override
    public abstract JSONObject getAllRegistedDeviceIds(BaseParam baseParam);

    /**
     * 添加设备
     * @param addDeviceParam
     * @return
     */
    @Override
    public abstract JSONObject addDevices(AddDeviceParam addDeviceParam);

    /**
     * 删除设备
     * @param deleteDevicceParam
     * @return
     */
    @Override
    public abstract JSONObject deleteDevices(DeleteDevicceParam deleteDevicceParam);

    /**
     * 获取所有设备
     * @return
     */
    @Override
    public abstract JSONObject listDevices(BaseParam baseParam);

    /**
     * 增加用户
     * @param addUserParam
     * @return
     */
    @Override
    public abstract JSONObject addUsers(AddUserParam addUserParam);

    /**
     * 删除用户
     * @param deleteUserParam
     * @return
     */
    @Override
    public abstract JSONObject deleteUser(DeleteUserParam deleteUserParam);

    /**
     * 获取所有用户
     * @return
     */
    @Override
    public abstract JSONObject listUsers(BaseParam baseParam);

    /**
     * 设置客户端版本
     * @param setClientVersionParam
     * @return
     */
    @Override
    public abstract JSONObject setClientVersion(SetClientVersionParam setClientVersionParam);

    /**
     * 设置客户端插件参数
     * @param setClientPluginsParamsParam
     * @return
     */
    @Override
    public abstract JSONObject setClientPluginsParams(SetClientPluginsParamsParam setClientPluginsParamsParam);

    /**
     * 获取客户端插件参数
     * @param clientBaseParam
     * @return
     */
    @Override
    public abstract JSONObject getClientPluginsParams(ClientBaseParam clientBaseParam);

    /**
     * 执行Mysql数据库命令
     * @param execSqlParam
     * @return
     */
    @Override
    public abstract JSONObject execSql(ExecSqlParam execSqlParam);

    /**
     * 执行GenericDB数据库命令
     * @param execGenericDbParam
     * @return
     */
    @Override
    public abstract JSONObject execGenericDB(ExecGenericDbParam execGenericDbParam);

    /**
     * 执行Mysql数据库命令
     * @param execMysqlParam
     * @return
     */
    @Override
    public abstract JSONObject execMysql(ExecMysqlParam execMysqlParam);

    /**
     * 执行Mssql数据库命令
     * @param execMssqlParam
     * @return
     */
    @Override
    public abstract JSONObject execMssql(ExecMssqlParam execMssqlParam);

    /**
     * 执行Oracle数据库命令
     * @param execOracleParam
     * @return
     */
    @Override
    public abstract JSONObject execOracle(ExecOracleParam execOracleParam);

    /**
     * 执行液位仪命令
     * @param execComParam
     * @return
     */
    @Override
    public abstract JSONObject execCom(ExecComParam execComParam);

    /**
     * 执行液位仪命令
     * @param execUdpParam
     * @return
     */
    @Override
    public abstract JSONObject execUdp(ExecUdpParam execUdpParam);

    /**
     * 执行opc命令
     * @param execOpcParam
     * @return
     */
    @Override
    public abstract JSONObject execOpc(ExecOpcParam execOpcParam);

    /**
     * 执行modbus命令
     * @param execModBusParam
     * @return
     */
    @Override
    public abstract JSONObject execModbus(ExecModBusParam execModBusParam);

    /**
     * 执行bacnet命令
     * @param execBacNetParam
     * @return
     */
    @Override
    public abstract JSONObject execBacnet(ExecBacNetParam execBacNetParam);

    /**
     * 执行系统命令
     * @param execSystemParam
     * @return
     */
    @Override
    public abstract JSONObject ExecSystem(ExecSystemParam execSystemParam);

    /**
     * 执行Http请求命令
     * @param execHttpParam
     * @return
     */
    @Override
    public abstract JSONObject execHttp(ExecHttpParam execHttpParam);

    /**
     * 执行Http请求命令
     * @param execPushDataParam
     * @return
     */
    @Override
    public abstract JSONObject execPushData(ExecPushDataParam execPushDataParam);

    /**
     * 关闭客户端
     * @param baseParam
     * @return
     */
    @Override
    public abstract JSONObject shutdownClient(BaseParam baseParam);

    /**
     * 获取客户端版本
     * @param baseParam
     * @return
     */
    @Override
    public abstract JSONObject viewClientVersion(BaseParam baseParam);

    /**
     * 获取客户端信息
     * @param baseParam
     * @return
     */
    @Override
    public abstract JSONObject viewClientInfomation(BaseParam baseParam);
}
